﻿using System;
using Microsoft.Win32;

namespace Foundation.ExtensionMethods
{
    public static class RegistryExtensions
    {
        /// <summary>
        /// Creates the or open sub key.
        /// </summary>
        /// <param name="rootKey">The root key.</param>
        /// <param name="path">The path.</param>
        /// <param name="writable">if set to <c>true</c> [writable].</param>
        /// <returns></returns>
        public static RegistryKey CreateOrOpenSubKey(this RegistryKey rootKey, string path, bool writable = true)
        {
            var subKey = rootKey.OpenSubKey(path, writable) ??
                         rootKey.CreateSubKey(path,
                                              writable
                                                  ? RegistryKeyPermissionCheck.ReadWriteSubTree
                                                  : RegistryKeyPermissionCheck.Default);
            if (subKey == null)
                throw new Exception("Can't open or create subkey.");
            return subKey;
        }

        /// <summary>
        /// Tries the create or open sub key.
        /// </summary>
        /// <param name="rootKey">The root key.</param>
        /// <param name="path">The path.</param>
        /// <param name="writable">if set to <c>true</c> [writable].</param>
        /// <returns></returns>
        public static RegistryKey TryCreateOrOpenSubKey(this RegistryKey rootKey, string path, bool writable = false)
        {
            try
            {
                return rootKey.CreateOrOpenSubKey(path, writable);
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Gets the value by path.
        /// </summary>
        /// <param name="rootKey">The root key.</param>
        /// <param name="path">The path.</param>
        /// <param name="valueName">Name of the value.</param>
        /// <returns></returns>
        public static object GetValueByPath(this RegistryKey rootKey, string path, string valueName = "")
        {
            using (var subKey = rootKey.OpenSubKey(path)) 
                return subKey == null ? null : subKey.GetValue(valueName);
        }

        /// <summary>
        /// Sets the value by path.
        /// </summary>
        /// <param name="rootKey">The root key.</param>
        /// <param name="path">The path.</param>
        /// <param name="valueName">Name of the value.</param>
        /// <param name="value">The value.</param>
        public static void SetValueByPath(this RegistryKey rootKey, string path, string valueName, object value)
        {
            if (value == null)
                value = string.Empty;

            using (var subKey = rootKey.CreateOrOpenSubKey(path))
                subKey.SetValue(valueName, value);
        }

        /// <summary>
        /// Existses the specified root key.
        /// </summary>
        /// <param name="rootKey">The root key.</param>
        /// <param name="path">The path.</param>
        /// <param name="valueName">Name of the value.</param>
        /// <returns></returns>
        public static bool Exists(this RegistryKey rootKey, string path, string valueName = "")
        {
            return rootKey.GetValueByPath(path, valueName) != null;
        }
    }
}
